<template>
  <a-card :bordered="false" class="card-area ccslh">
    <div class="table-page-search-wrapper" v-if="!visible">
      <a-form layout="vertical">
        <a-row :gutter="48">
          <a-col :md="6" :sm="12">
            <a-form-item label="姓名">
              <a-input v-model="queryParam.xm"/>
            </a-form-item>
          </a-col>
          <!-- <a-col :md="6" :sm="12">
            <a-form-item label="开始时间">
              <a-date-picker v-model="queryParam.start_time" />
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="12">
            <a-form-item label="结束时间">
              <a-date-picker v-model="queryParam.ent_time" />
            </a-form-item>
          </a-col> -->
          <a-col :md="6" :sm="12">
              <a-form-item label="院系">
                <a-select :disabled="selectMenu.yx.length === 1" :allowClear="true" showSearch :filterOption="filterOption" v-model="queryParam.yxm" @change="yxChange">
                  <a-select-option v-for="i in selectMenu.yx" :key="i.id" :value="i.dwh">{{ i.dwh }} {{ i.dwmc }}</a-select-option>
                </a-select>
              </a-form-item>
          </a-col>
          <a-col :md="6" :sm="12">
              <a-form-item label="专业">
                <a-select :allowClear="true" showSearch :filterOption="filterOption" v-model="queryParam.zym">
                  <a-select-option v-for="i in selectMenu.zy" :key="i.id" :value="i.zyh">{{ i.zyh}} {{ i.zymc }}</a-select-option>
                </a-select>
              </a-form-item>
          </a-col>
          <a-col :md="6" :sm="12">
              <a-form-item label="审核状态">
                <a-select allowClear v-model="queryParam.autstate">
                  <a-select-option v-for="i in TYPE0402" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
          </a-col>
          <a-col :md="8" :sm="16">
            <span class="table-page-search-submitButtons" >
              <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
              <!-- <a-button type="primary" @click="exportExcel">导出</a-button> -->
              <a-button type="primary" @click="modelShow = true">发起贷款申请</a-button>
              <a-button style="margin-left: 8px" @click="() => queryParam = {yxm: selectMenu.yx.length === 1 ? queryParam.yxm : ''}">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <s-table
      v-if="!visible"
      ref="table"
      size="default"
      rowKey="key"
      :columns="columns"
      :data="loadData"
      :alert="options.alert"
      :pageSize="parameter.pageSize"
      :pageNum="parameter.pageNum"
      :scroll="{x: 1600}"
    >

      <template slot="xh" slot-scope="a,b">
         <a @click="direct(b)">{{a}}</a>
      </template>
      <template slot="operation" slot-scope="a,b">
        <a @click="cancelRequest(b)">删除</a>
      </template>
    </s-table>

    <a-modal
      title="请输入学号"
      :visible="modelShow"
      :confirm-loading="confirmLoading"
      @ok="handleOk"
      @cancel="handleCancel"
    >
      <a-form layout="vertical">
        <a-row :gutter="24">
          <a-col :md="24" :sm="24">
            <a-form-item>
              <a-input placeholder="请输入学号" v-model="queryParam.studentId"/>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-modal>

    <div v-if="visible">
      <a-form :form='form'>
        <a-row :gutter="48">
          <a-col :md="6" :sm="12">
            <a-form-item label="姓名" :labelCol="{span: 24,offset: 0}" :wrapperCol="{span: 24}">
              <a-input disabled v-model="studentInfo.xjxx.xm"/>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="12">
            <a-form-item label="院系" :labelCol="{span: 24,offset: 0}" :wrapperCol="{span: 24}">
              <a-input disabled v-model="studentInfo.xjxx.ssyxmc"/>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="12">
            <a-form-item label="专业" :labelCol="{span: 24,offset: 0}" :wrapperCol="{span: 24}">
              <a-input disabled v-model="studentInfo.xjxx.sszymc"/>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="12">
            <a-form-item label="性别" :labelCol="{span: 24,offset: 0}" :wrapperCol="{span: 24}">
              <a-input disabled v-model="studentInfo.xbmc"/>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="12">
            <a-form-item label="年级" :labelCol="{span: 24,offset: 0}" :wrapperCol="{span: 24}">
              <a-input disabled v-model="studentInfo.xjxx.sznj"/>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="12">
            <a-form-item label="导师姓名" :labelCol="{span: 24,offset: 0}" :wrapperCol="{span: 24}">
              <a-input disabled v-model="studentInfo.xjxx.dsxm"/>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="12">
            <a-form-item label="学生类别" :labelCol="{span: 24,offset: 0}" :wrapperCol="{span: 24}">
              <a-input disabled v-model="studentInfo.xjxx.xslbmc"/>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="12">
            <a-form-item label="培养方式" :labelCol="{span: 24,offset: 0}" :wrapperCol="{span: 24}">
              <a-input disabled v-model="studentInfo.xjxx.pyfsmc"/>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="12">
              <a-form-item label="学年" :labelCol="{span: 24,offset: 0}" :wrapperCol="{span: 24}">
                <a-select allowClear v-decorator="[
                  'dknd',
                  {
                    rules: [{ required: true, message: '请选择学年' }],
                  }
                ]">
                  <a-select-option v-for="i in TYPE0125" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
          </a-col>
          <a-col :md="6" :sm="12">
            <a-form-item label="贷款金额" :labelCol="{span: 24,offset: 0}" :wrapperCol="{span: 24}">
                <a-input type="text" v-decorator="[
                  'dkje',
                  {
                    rules: [{ required: true, message: '请输入时长' }],
                  }
                ]"></a-input>
            </a-form-item>
          </a-col>
        </a-row>
        <!-- <a-row>
          <a-form-item label="备注" :labelCol="{span: 24,offset: 0}" :wrapperCol="{span: 24}">
              <a-input type="textarea" v-decorator="[
                  'leaveRemark',
                  {
                    rules: [{ required: false, }],
                  }
                ]" :autosize="{ minRows: 6, maxRows: 10 }"/>
            </a-form-item>
        </a-row> -->
        <a-row style="text-align: center">
          <a-button type="primary" @click="sub">提交审核</a-button>
          <a-button type="primary" @click="closeSub">关闭</a-button>
        </a-row>
      </a-form>
    </div>
  </a-card>
</template>

<script>
// import moment from 'moment'
import { STable } from '~'
import { mapState } from 'vuex'
import moment from 'moment'

export default {
  name: 'TableList',
  components: {
    STable
  },
  data () {
    return {
      modelShow:false,
      // 高级搜索 展开/关闭
      advanced: true,
      // 查询参数
      queryParam: {},
      studentInfo:{},//学生信息
      postParams: {},
      stuInfo: {},
      // 表头
      columns: [
        // {
        //   title: '序号',
        //   scopedSlots: { customRender: 'serial' }
        // },
        {
          title: '姓名',
          dataIndex: 'xm',
          width:100
        },
        {
          title: '院系',
          dataIndex: 'yxm',
        },
        {
          title: '专业',
          dataIndex: 'zym',
        },
        {
          title: '贷款学年',
          dataIndex: 'dknd'
        },
        {
          title: '贷款日期',
          dataIndex: 'createDate'
        },
        {
          title: '贷款金额',
          dataIndex: 'dkje',
        },
        {
          title: '审核状态',
          dataIndex: 'autstate',
        },
        // {
        //   title: '操作',
        //   fixed: 'right',
        //   scopedSlots: { customRender: 'operation' },
        //   width: '150px'
        // }
      ],
      parameter: {
        pageNo: 1,
        pageSize: 10
      },
      selectMenu: {
        yx: [], // 院系
        zy: [] // 所属专业
      },
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.parameter = parameter
        parameter.pageNum = parameter.pageNo
        let params = Object.assign(this.queryParam, parameter)
        return this.$api.student.studentloanList(params).then(res => {
          if(res.data.rows){
             res.data.rows.map((item=>{
                item.createDate = moment(item.createDate).format('YYYY-MM-DD')
                //item.entTime = moment(item.entTime).format('YYYY-MM-DD')
             }))
          } 
          return {
            data: res.data.rows || [],
            pageNo: parameter.pageNo,
            pageSize: parameter.pageSize,
            totalPage: Math.ceil(res.data.total / parameter.pageSize),
            totalCount: res.data.total
          }

        })
      },
      selectedRowKeys: [],
      selectedRows: [],

      // custom table alert & rowSelection
      options: {
        alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange
        }
      },
      optionAlertShow: true, // 是否显示alert，已选中
      visible: false,
      confirmLoading: false,
      editId: null,
    }
  },
  computed: {
    ...mapState({
      TYPE0125: state => state.app['TYPE0125'],
      TYPE0402: state => state.app['TYPE0402'],
    })
  },
  created () {
    this.$store.dispatch('app/sysInfo').then(data => {
      this.$set(this.queryParam, 'xn', data.schoolYear)
    })
    this.$store.dispatch('app/setDictionary', 'TYPE0125')
    this.$store.dispatch('app/setDictionary', 'TYPE0402')
    this.initSelectMenu()
  },
  beforeCreate () {
    this.form = this.$form.createForm(this)
  },
  methods: {
    refresh() {
      this.$refs.table.refresh(false)
    },
    initSelectMenu () {
      let allRequest = [
        this.$api.base.collegeAll()
      ]
      Promise.all(allRequest).then(res => {
        this.selectMenu = {
          yx: res[0].data
        }
        if(this.selectMenu.yx && this.selectMenu.yx.length == 1){
          this.$set(this.queryParam, 'yxm', this.selectMenu.yx[0].dwh)
          this.yxChange(this.queryParam.yxm)
        }
        this.initDone = true
      })
    },
    yxChange (code) {
      this.$set(this.queryParam, 'zym', '')
      if (code) {
        this.$api.base.majorAll({lsdwh: code}).then(res => {
          this.$set(this.selectMenu, 'zy', res.data)
        })
      } else {
        this.$set(this.selectMenu, 'zy', [])
      }
    },
    handleOk(){

      this.$api.student.studentInfo(this.queryParam.studentId).then(res => {
          this.studentInfo = res.data
          this.visible = true
          this.modelShow = false
        }).catch(e => {

        })
    },
    handleCancel(e) {
      this.modelShow = false;
      this.queryParam.studentId = ''
    },
    sub () {
      const form = this.form
      form.validateFields((err, values) => {
        if (err) {
          return
        }
        let params = Object.assign(values)
        params.autstate = '01'
        params.xh = this.queryParam.studentId
        this.$DateToString(params)
        this.$api.student.studentLoanAdd(params).then(res => {
          this.$refs.table.refresh(true)
          form.resetFields()
          this.visible = false
        }).catch(e => {
          this.visible = false
          this.$message.success(res.msg)
          this.$refs.table.refresh(true)
          form.resetFields()
        })
      })
      this.queryParam.studentId = ''
    },
    closeSub(){
      this.visible=false
      this.queryParam.studentId = ''
    },
    filterOption (value, op) {
      return op.componentOptions.children[0].text.indexOf(value) !== -1
    },
    // direct (data) {
    //   this.$router.push(`/student/stuStatus/search/stuView?xh=${data.xh}`)
    // },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    cancelRequest (row) {
      this.$api.student.studentLeaveCancel(row.id).then(res => {
        this.$message.success(res.msg)
        this.$refs.table.refresh(true)
      })
    },
  }
}
</script>
<style lang="less">
.ccslh{
      table {
        table-layout:fixed;
      }
      tr td:nth-child(4){
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
      }
      tr td:nth-child(5){
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
      }
}

</style>
